package gizwits.centerair.way;

import java.util.Arrays;

import gizwits.kit.ByteUtil;

// BIO nio nio2==>aio 
public class Test {

	public static void main(String[] args) {
//		StringBuffer sb = new StringBuffer();
//		sb.append(1231).append(",");
//		System.out.println(sb.insert(0, "asasa"));
		byte[] arr ={(byte)0x19};
//		ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//		for (int i = 0; i < arr.length; i++) {
//			byteBuffer.put(i, arr[i]);
//		}
//		int a1 = byteBuffer.get();
//		
//		int a2 = byteBuffer.get();
//		int a3 = byteBuffer.getInt();
//		System.out.println(a2+" ");
		byte[] bs = concatAll(new byte[]{}, toByte(18,1));
		for(byte b : bs){
			System.out.println(b);
		}
		System.out.println();
		ByteUtil.printBytes(bs);
		System.out.println(ByteUtil.toBinaryStr(18, 8));
		System.out.println(ByteUtil.toBinaryStr(18, 8).replaceFirst("0", "1"));
		ByteUtil.printBytes(ByteUtil.toBinarys(ByteUtil.toBinaryStr(20, 8).replaceFirst("0", "1")));
		
		System.out.println(toIntByLeft2((byte)0xA0));
		
		System.out.println(ByteUtil.toInt(arr, 0, 1));
		
		ByteUtil.printBytes(ByteUtil.hexStringToBytes("5243B195964CF8250FF9E8D7148EFC32"));
		
		System.out.println(ByteUtil.printHexString(toByte(998082,4)));
		
		System.out.println(String.format("did:%d,homeId:%d,cid:%d", 12,312,12));
	}

	/**
	 * 把【一个字节中的几个bit】转【整数】,方向【向左】<
	 */
	public static int toIntByLeft(byte b) {
		return (b>>5)&0x07;
	}
	
	
	public static int toIntByLeft2(byte b) {
		return (b<<2)&0x01;
	}
	
	public static int toInt(byte[] bs,int start,int end){
		int num = 0;
		int len = end - start;
		for(int i=0;i<len;i++){
			num = num | ((bs[i+start] & 0xff) << i * 8);
		}
	    return num;  
	}
	
	/**
	 * 【小端格式】【数字】转【字节数组】
	 */
	public static byte[] toByte(int num,int len){
		byte[] bs = new byte[len];
		for(int i=0;i<len;i++){
			bs[i] = (byte)(0xff & (num >> (i*8)));
		}
		return bs;
	}
	
	public static byte[] concatAll(byte[] first, byte[]... arrays) {
		  int totalLength = first.length;  
		  for (byte[] array : arrays) {  
		    totalLength += array.length;  
		  }  
		  byte[] result = Arrays.copyOf(first, totalLength);
		  int offset = first.length;  
		  for (byte[] array : arrays) {  
		    System.arraycopy(array, 0, result, offset, array.length);  
		    offset += array.length;  
		  }  
		  return result;  
	}
	
	/**
	 * 把所有【字节数组】合并;【计算【所有字节数组的总长度】并添加到【结果数组的头部】】长度格式【小端格式下的两个字节】
	 * @param os
	 * @return
	 */
	public static byte[] toResult(byte[]... arrays){
		int len = 2;	//len的长度为2
		for(byte[] array : arrays){
			len += array.length;
		}
		byte[] first = toByte(len,2);
		byte[] result = concatAll(first,arrays);
		return result;
	}
}
